import storage from '@/lib/utils/storage'
import {
	STORE_KEY_TOKEN,
	STORE_KEY_UID,
	STORE_KEY_USER_ADDRESS
} from '@/config/constants'

export const SET_TOKEN = 'SET_TOKEN'
export const USE_RINFO = 'USE_RINFO'
export default {
	state: {
		token: storage.get(STORE_KEY_TOKEN),
		userInfo:storage.getJSON('STORE_KEY_USER_INFO') || {}, 
		// userAddress: storage.get(STORE_KEY_USER_ADDRESS)
	},

	mutations: {
		[SET_TOKEN]: (state, token) => {
			state.token = token
		},
		'USE_RINFO': (state, USER) => {
			state.userInfo = USER;
			storage.setJSON('STORE_KEY_USER_INFO',USER); // 同步存储到本地
		},
		
		// [SET_USER_ADDRESS]: (state, userAddress) => {
		// state.userAddress = userAddress
		// }
	},

	actions: {
		// 登录
		Login({
			commit
		}, {
			token
		}) {
			storage.set(STORE_KEY_TOKEN, token)
			commit(SET_TOKEN, token)
		},
		// 退出
		LogOut({
			commit
		}) {
			uni.hideLoading()
			uni.hideToast()
			storage.remove(STORE_KEY_TOKEN, );
			commit(SET_TOKEN, '');
			storage.remove('STORE_KEY_USER_INFO', )
			commit('USE_RINFO', '')
		},
		
	}
}